home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / sync / syncLockStat.c < prev    next >
C/C++ Source or Header  |  1990-10-05  |  13KB  |  529 lines

  1. /* 
  2.  * syncLockStat.c --
  3.  *
  4.  *     Maintain statistics about lock usage and lock dependencies.
  5.  *
  6.  * Copyright 1989 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/kernel/sync/RCS/syncLockStat.c,v 9.1 90/10/05 17:50:34 mendel Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <sync.h>
  21. #include <syncInt.h>
  22. #include <stdlib.h>
  23. #include <dbg.h>
  24. #include <stdio.h>
  25.  
  26. /*
  27.  * Lock registration semaphore.
  28.  */
  29. Sync_Semaphore regMutex = Sync_SemInitStatic("regMutex");
  30. Sync_Semaphore *regMutexPtr = ®Mutex;
  31.  
  32. #ifdef LOCKREG
  33. /*
  34.  * Number of types registered.
  35.  */
  36. static     int    syncTypeCount = 0;
  37. /*
  38.  * Information on each type of lock registered.
  39.  */
  40. static  Sync_RegElement        regInfo[SYNC_MAX_LOCK_TYPES];
  41. #endif
  42. static     Boolean    initialized = FALSE;
  43.  
  44. /*
  45.  * Keep track of locks we see with bad types. This is for debugging purposes
  46.  * only.
  47.  */
  48. #define  MAX_BAD_TYPES    100
  49. struct  BadLockType {
  50.     Address        lockPtr;
  51.     Address        pc;
  52.     int        type;
  53. }  badType[MAX_BAD_TYPES];
  54.  
  55. int        badTypeCount = 0;
  56.  
  57.  
  58.  
  59. /*
  60.  *----------------------------------------------------------------------
  61.  *
  62.  * Sync_LockStatInit --
  63.  *
  64.  *    Initializes the lock statistics routines. Must be called before
  65.  *    any kernel processes are created, and after the proc module
  66.  *    is initialized.
  67.  *
  68.  * Results:
  69.  *    None.
  70.  *
  71.  * Side effects:
  72.  *    None.
  73.  *
  74.  *----------------------------------------------------------------------
  75.  */
  76.  
  77. void
  78. Sync_LockStatInit()
  79. {
  80.     initialized = TRUE;
  81.     /*
  82.      * regMutex can't be treated like a normal lock otherwise we have a
  83.      * chicken and the egg problem.
  84.      */
  85. #ifdef LOCKREG
  86.     regMutex.type = -1; 
  87. #endif
  88. }
  89.  
  90.  
  91. /*
  92.  *----------------------------------------------------------------------
  93.  *
  94.  * SyncAddPriorInt --
  95.  *
  96.  *    Adds the prior lock  to the list of prior locks  in the 
  97.  *    current lock. Adds the current lock to the stack of 
  98.  *    locks in the pcb. The current lock is registered if it hasn't
  99.  *    been already.
  100.  *
  101.  * Results:
  102.  *    None.
  103.  *
  104.  * Side effects:
  105.  *    The lock stack in the pcb is changed.
  106.  *
  107.  *----------------------------------------------------------------------
  108.  */
  109.  
  110. /*ARGSUSED*/
  111.  
  112. void
  113. SyncAddPriorInt(type, priorCountPtr, priorTypes, lockPtr, pcbPtr)
  114.     int                type;
  115.     int             *priorCountPtr;
  116.     int                *priorTypes;
  117.     Address            lockPtr;
  118.     Proc_ControlBlock        *pcbPtr;
  119. {
  120. #ifdef LOCKDEP
  121.     int            priorType;
  122.     int            i;
  123.     Address        priorLockPtr;
  124.     static Boolean    firstOverflow = TRUE;
  125.  
  126.     if (pcbPtr == (Proc_ControlBlock *) NIL || !initialized) {
  127.     return;
  128.     }
  129.  
  130.     Proc_GetCurrentLock(pcbPtr, &priorType, &priorLockPtr);
  131.     if (priorType > syncTypeCount) {
  132.     if (badTypeCount < MAX_BAD_TYPES) {
  133.         badType[badTypeCount].lockPtr = priorLockPtr;
  134.         badType[badTypeCount].pc = FIELD(priorLockPtr, holderPC);
  135.         badType[badTypeCount].type = priorType;
  136.         badTypeCount++;
  137.     }
  138.     }
  139.     if (priorType >= 0) {
  140.     for (i = 0; i < *priorCountPtr; i++) {
  141.         if (priorType == priorTypes[i]) {
  142.         break;
  143.         }
  144.     }
  145.     if (i == *priorCountPtr && i < SYNC_MAX_PRIOR) {
  146.         priorTypes[i] = priorType;
  147.         *priorCountPtr += 1;
  148.     } else if (i >= SYNC_MAX_PRIOR && firstOverflow) {
  149.         printf("SyncAddPrior: too many prior types.\n");
  150.         firstOverflow = FALSE;
  151.     }
  152.     }
  153.     if (type == 0) {
  154.     Sync_LockRegister(lockPtr);
  155. /* this routine never gets called if LOCKREG is not defined, but lint 
  156.  * will complain about this assignment anyway.
  157.  */
  158.     type = FIELD(lockPtr, type);
  159.     }
  160.     Proc_PushLockStack(pcbPtr, type, lockPtr);
  161. #endif
  162. }
  163.  
  164. /*
  165.  *----------------------------------------------------------------------
  166.  *
  167.  * SyncDeleteCurrentInt --
  168.  *
  169.  *    Removes a prior lock from the lock stack.
  170.  *
  171.  * Results:
  172.  *    None.
  173.  *
  174.  * Side effects:
  175.  *    The lock stack in the pcb is changed.
  176.  *
  177.  *----------------------------------------------------------------------
  178.  */
  179.  
  180. void
  181. SyncDeleteCurrentInt(lockPtr, pcbPtr)
  182.     Address            lockPtr;
  183.     Proc_ControlBlock        *pcbPtr;
  184. {
  185.     if (pcbPtr == (Proc_ControlBlock *) NIL || !initialized) {
  186.     return;
  187.     }
  188.     Proc_RemoveFromLockStack(pcbPtr, lockPtr); 
  189. }
  190.  
  191. /*
  192.  *----------------------------------------------------------------------
  193.  *
  194.  *  SyncMergePriorInt --
  195.  *
  196.  *    Merge the prior entries for a given lock with the prior entries
  197.  *    for the type. If an entry is a duplicate it is discarded, and
  198.  *    if the prior entry database overflows an error message is printed.
  199.  *
  200.  * Results:
  201.  *    None.
  202.  *
  203.  * Side effects:
  204.  *    Stuff might be printed to the screen.
  205.  *
  206.  *----------------------------------------------------------------------
  207.  */
  208. void
  209. SyncMergePriorInt(priorCount, priorTypes, regPtr)
  210.     int         priorCount;
  211.     int            *priorTypes;
  212.     Sync_RegElement    *regPtr;
  213. {
  214.     int        i;
  215.     int        j;
  216.  
  217.     if (!initialized) {
  218.     return;
  219.     }
  220.     for (i = 0; i < priorCount; i++) {
  221.     for (j = 0; j < regPtr->priorCount; j++ ) {
  222.         if (regPtr->priorTypes[j] == priorTypes[i]) {
  223.         break;
  224.         }
  225.     }
  226.     if (j == regPtr->priorCount) {
  227.         if (j == SYNC_MAX_PRIOR) {
  228.         break;
  229.         }
  230.         regPtr->priorTypes[j] = priorTypes[i];
  231.         regPtr->priorCount++;
  232.     }
  233.     }
  234.     if (i < priorCount) {
  235.     printf("SyncMergePriorInt: %d too many prior types.\n",
  236.            priorCount - i);
  237.     }
  238. }
  239.  
  240. /*
  241.  *----------------------------------------------------------------------
  242.  *
  243.  * Sync_RegisterInt --
  244.  *
  245.  *    Registers a lock of either class (semaphore or lock). If an element
  246.  *    of the type exists then the lock is added to the linked list of 
  247.  *    active locks of that type. Type equality is determined by comparing
  248.  *    the ascii name of the locks. If the lock is of a new type then an
  249.  *    element for the type is added to the list and the new lock is added
  250.  *    to the element.
  251.  *
  252.  *    This routine should be called prior to using the lock.
  253.  *
  254.  * Results:
  255.  *    None.
  256.  *
  257.  * Side effects:
  258.  *    A new element may be added to regQueue.
  259.  *
  260.  *----------------------------------------------------------------------
  261.  */
  262.  
  263. #ifndef LOCKREG
  264. /* ARGSUSED */
  265. #endif
  266.  
  267. void
  268. Sync_RegisterInt(lock)
  269.     Address        lock;        /*lock to be registered */
  270. {
  271. #ifdef LOCKREG
  272.     List_Links        *lockQueuePtr;
  273.     Sync_RegElement    *regPtr;
  274.     char        *name;
  275.     static int        newTypeGenerator = 1;
  276.     int            *typePtr;
  277.     Sync_ListInfo    *listInfoPtr;
  278.     int            i;
  279.  
  280.     typePtr = &(FIELD(lock,type)) ;
  281.     if (*typePtr != 0) {
  282.     return;
  283.     }
  284.     name = FIELD(lock,name);
  285.     if (name == (char *) 0) {
  286.     return;
  287.     }
  288.     if (initialized) {
  289.     MASTER_LOCK(regMutexPtr);
  290.     }
  291.     if (*typePtr != 0 || FIELD(lock, name) == (char *) 0) {
  292.     goto exit;
  293.     }
  294.     listInfoPtr = &(FIELD(lock,listInfo));
  295.     regPtr = (Sync_RegElement *) NIL;
  296.     for (i = 0; i < syncTypeCount; i++) {
  297.     if (!strcmp(name, regInfo[i].name)) {
  298.         regPtr = ®Info[i];
  299.         regPtr->activeLockCount++;
  300.         break;
  301.     }
  302.     }
  303.     if (regPtr == (Sync_RegElement *) NIL) {
  304.     if (syncTypeCount >= SYNC_MAX_LOCK_TYPES) {
  305.         printf("Sync_RegisterAnyLock: too many lock types.\n");
  306.         goto exit;
  307.     }
  308.     regPtr = ®Info[syncTypeCount];
  309.     regPtr->name = name;
  310.     regPtr->type = newTypeGenerator++;
  311.     regPtr->activeLockCount = 1;
  312.     regPtr->deadLockCount = 0;
  313.     regPtr->hit = 0;
  314.     regPtr->miss = 0;
  315.     regPtr->priorCount = 0;
  316.     List_Init((List_Links *) &(regPtr->activeLocks));
  317.     syncTypeCount++;
  318.     }
  319.     lockQueuePtr = (List_Links *) &(regPtr->activeLocks);
  320.     if (listInfoPtr == (Sync_ListInfo *) lockQueuePtr->prevPtr) {
  321.     panic("Trying to reregister a lock.\n");
  322.     }
  323.     regPtr->class = ((Sync_Lock *) lock)->class;
  324.     *typePtr = regPtr->type;
  325.     listInfoPtr->lock = lock;
  326.     List_InitElement((List_Links *) listInfoPtr);
  327.     List_Insert((List_Links *) listInfoPtr, 
  328.         LIST_ATREAR(lockQueuePtr));
  329. exit:
  330.     if (initialized) {
  331.     MASTER_UNLOCK(regMutexPtr);
  332.     }
  333. #endif /* LOCKREG */
  334. }
  335.  
  336. /*
  337.  *----------------------------------------------------------------------
  338.  *
  339.  * Sync_CheckoutInt --
  340.  *
  341.  *    Used to de-register ("checkout") a lock when it is being
  342.  *    deallocated. It is removed from the linked list of active locks for
  343.  *    the type, and its statistics are merged with the running total in
  344.  *    the type element.
  345.  *
  346.  * Results:
  347.  *    None.
  348.  *
  349.  * Side effects:
  350.  *    None.
  351.  *
  352.  *----------------------------------------------------------------------
  353.  */
  354.  
  355. #ifndef LOCKREG
  356. /*ARGSUSED*/
  357. #endif
  358.  
  359. void
  360. Sync_CheckoutInt(lock)
  361.     Address        lock;        /*lock to be registered */
  362. {
  363. #ifdef LOCKREG
  364.     List_Links        *lockQueuePtr;
  365.     List_Links        *itemPtr;
  366.     Sync_RegElement    *regPtr;
  367.     int            type;
  368.  
  369.     if (initialized) {
  370.     MASTER_LOCK(regMutexPtr);
  371.     }
  372.     type = FIELD(lock,type);
  373.     if (type <= 0) {
  374.     goto exit;
  375.     }
  376.     regPtr = ®Info[type-1];
  377.     lockQueuePtr = (List_Links *) &(regPtr->activeLocks);
  378.     LIST_FORALL(lockQueuePtr, itemPtr) {
  379.     if (((Sync_ListInfo *) itemPtr)->lock == lock) {
  380.         List_Remove(itemPtr);
  381.         regPtr->activeLockCount--;
  382.         regPtr->deadLockCount++;
  383.         SyncAddLockStats(regPtr, ((Sync_ListInfo *) itemPtr)->lock);
  384.         goto exit;
  385.     }
  386.     }
  387. exit:
  388.     if (initialized) {
  389.     MASTER_UNLOCK(regMutexPtr);
  390.     }
  391. #endif /* LOCKREG */
  392. }
  393.  
  394. /*
  395.  *----------------------------------------------------------------------
  396.  *
  397.  * Sync_GetLockStats --
  398.  *
  399.  *    Prints out the locking statistics.
  400.  *
  401.  * Results:
  402.  *    None.
  403.  *
  404.  * Side effects:
  405.  *    None.
  406.  *
  407.  *----------------------------------------------------------------------
  408.  */
  409.  
  410. #ifndef LOCKREG
  411. /*ARGSUSED*/
  412. #endif
  413.  
  414. ReturnStatus
  415. Sync_GetLockStats(size, argPtr)
  416.     int         size;
  417.     Address    argPtr;
  418.  
  419. {
  420.  
  421. #ifdef LOCKREG
  422.     List_Links        *lockQueuePtr;
  423.     List_Links        *itemPtr;
  424.     Sync_RegElement    *regPtr;
  425.     Sync_RegElement    tempReg;
  426.     int            i;
  427.     int            j;
  428.     Sync_LockStat    *statArray;
  429.     int            index;
  430.     ReturnStatus    status;
  431.  
  432.     if (size <= 0) {
  433.     return SUCCESS;
  434.     }
  435.     if (size < syncTypeCount) {
  436.     return FAILURE;
  437.     }
  438.     statArray = (Sync_LockStat *) malloc(size * sizeof(Sync_LockStat));
  439.     MASTER_LOCK(regMutexPtr);
  440.     bzero((char *) statArray, size * sizeof(Sync_LockStat));
  441.     for (i = 0; i < syncTypeCount; i++) {
  442.     regPtr = ®Info[i];
  443.     bcopy((char *) regPtr, (char *) &tempReg, sizeof(Sync_RegElement));
  444.     lockQueuePtr = (List_Links *) &(regPtr->activeLocks);
  445.     LIST_FORALL(lockQueuePtr, itemPtr) {
  446.         SyncAddLockStats(&tempReg, ((Sync_ListInfo *) itemPtr)->lock);
  447.     }
  448.     index = regPtr->type -1;
  449.     statArray[index].inUse = 1;
  450.     statArray[index].class = (regPtr->class == SYNC_SEMAPHORE) ? 0 : 1;
  451.     statArray[index].hit = tempReg.hit;
  452.     statArray[index].miss = tempReg.miss;
  453.     statArray[index].activeCount = regPtr->activeLockCount;
  454.     statArray[index].deadCount = regPtr->deadLockCount;
  455.     strncpy(statArray[index].name, regPtr->name, 30);
  456.     statArray[index].name[29] = '\0';
  457.     statArray[index].priorCount = tempReg.priorCount;
  458.     for (j = 0; j < tempReg.priorCount; j++) {
  459.         statArray[index].priorTypes[j] = tempReg.priorTypes[j];
  460.     }
  461.     for (j = 0; j < mach_NumProcessors; j++) {
  462.         statArray[index].spinCount += sync_Instrument[j].spinCount[index+1];
  463.     }
  464.     }
  465.     Vm_CopyOut(sizeof(Sync_LockStat) * size, (Address)statArray, argPtr);
  466.     MASTER_UNLOCK(regMutexPtr);
  467.     free((Address) statArray);
  468.     return (SUCCESS);
  469. #else  /* LOCKREG */
  470.     return (FAILURE);
  471. #endif /* LOCKREG */
  472. }
  473.  
  474. /*
  475.  *----------------------------------------------------------------------
  476.  *
  477.  * Sync_ResetLockStats --
  478.  *
  479.  *    Resets all the locking statistics.
  480.  *
  481.  * Results:
  482.  *    FAILURE if an error occurred, SUCCESS otherwise.
  483.  *
  484.  * Side effects:
  485.  *    Hit and miss counts are reset, count of prior lock types is
  486.  *    reset.
  487.  *
  488.  *----------------------------------------------------------------------
  489.  */
  490.  
  491. ReturnStatus
  492. Sync_ResetLockStats()
  493. {
  494. #ifdef LOCKREG
  495.     int         i;
  496.     int            j;
  497.     List_Links        *lockQueuePtr;
  498.     List_Links        *itemPtr;
  499.     Sync_RegElement    *regPtr;
  500.  
  501.     MASTER_LOCK(regMutexPtr);
  502.     for (i = 0; i < syncTypeCount; i++) {
  503.     regPtr = ®Info[i];
  504.     lockQueuePtr = (List_Links *) &(regPtr->activeLocks);
  505.     LIST_FORALL(lockQueuePtr, itemPtr) {
  506.         *(&FIELD(((Sync_ListInfo *) itemPtr)->lock, hit)) = 0;
  507.         *(&FIELD(((Sync_ListInfo *) itemPtr)->lock, miss)) = 0;
  508. #ifdef LOCKDEP
  509.         *(&FIELD(((Sync_ListInfo *) itemPtr)->lock, priorCount)) = 0;
  510. #endif
  511.     }
  512.     regPtr->hit = 0;
  513.     regPtr->miss = 0;
  514.     regPtr->priorCount = 0;
  515.     }
  516.     for (i = 0; i < mach_NumProcessors; i++) {
  517.     for (j = 0; j < syncTypeCount+1; j++) {
  518.         sync_Instrument[i].spinCount[j] = 0;
  519.     }
  520.     sync_Instrument[i].sched_MutexMiss = 0;
  521.     }
  522.     MASTER_UNLOCK(regMutexPtr);
  523.     return SUCCESS;
  524. #else  /* LOCKREG */
  525.     return (FAILURE);
  526. #endif /* LOCKREG */
  527.  
  528. }
  529.